package com.asura.framework.rulemodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 树节点.
 * <ul>
 * <li>父节点{@code parent}为空, 代表该节点为树根.</li>
 * <li>子节点{@code childs}有值, 代表该节点为树枝.</li>
 * <li>子节点{@code childs}为空, 代表该节点为树叶.</li>
 * </ul>
 * 
 * @author a123
 *
 */
public class TreeNode {

	private String name;

	private String group;

	private int type;

	private String note;

	private String front;

	private int optional;

	// private TreeNode parent;

	private List< TreeNode > childs = new ArrayList< TreeNode >( 3 );

	public TreeNode( )
	{
	}

	public TreeNode( String name )
	{
		this.name = name;
	}

	public String getName( )
	{
		return name;
	}

	public void setName( String name )
	{
		this.name = name;
	}

	public String getGroup( )
	{
		return group;
	}

	public void setGroup( String group )
	{
		this.group = group;
	}

	public int getType( )
	{
		return type;
	}

	public void setType( int type )
	{
		this.type = type;
	}

	public String getNote( )
	{
		return note;
	}

	public void setNote( String note )
	{
		this.note = note;
	}

	public int getOptional( )
	{
		return optional;
	}

	public void setOptional( int optional )
	{
		this.optional = optional;
	}

	// public TreeNode getParent( )
	// {
	// return parent;
	// }
	//
	// public void setParent( TreeNode parent )
	// {
	// this.parent = parent;
	// }

	public List< TreeNode > getChilds( )
	{
		return childs;
	}

	public void setChilds( List< TreeNode > childs )
	{
		this.childs = childs;
	}

	/**
	 * 是否为叶子节点.
	 * 
	 * @return
	 */
	public boolean isLeaf( )
	{
		return childs == null || childs.isEmpty( );
	}

	public String getFront( )
	{
		return front;
	}

	public void setFront( String front )
	{
		this.front = front;
	}

	@Override
	public int hashCode( )
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ( ( name == null ) ? 0 : name.hashCode( ) );
		return result;
	}

	@Override
	public boolean equals( Object obj )
	{
		if ( this == obj )
			return true;
		if ( obj == null )
			return false;
		if ( getClass( ) != obj.getClass( ) )
			return false;
		TreeNode other = ( TreeNode ) obj;
		if ( name == null )
		{
			if ( other.name != null )
				return false;
		} else if ( !name.equals( other.name ) )
			return false;
		return true;
	}

	@Override
	public String toString( )
	{
		StringBuilder tostr = new StringBuilder( );
		tostr.append( "[name=" ).append( name );
		tostr.append( ", group=" ).append( group );
		tostr.append( ", type=" ).append( RuleTypeEnum.getRuleType( type ) );
		tostr.append( ", note=" ).append( note );
		tostr.append( ", optional=" ).append( optional );
		// tostr.append( ", parent=" ).append( parent == null ? "N/A" : parent
		// );

		tostr.append( ", childs=" ).append( Objects.toString( childs, "[]" ) );
		if ( isLeaf( ) )
		{
			tostr.append( ", leaf=" ).append( "叶子节点" );
		}
		tostr.append( "]" );
		return tostr.toString( );
	}
}
